import Vue from 'vue'
import VueRouter from 'vue-router'

import { getItem } from '@/utils/storage'

import Layout from '@/views/layout.vue'
// import Register from '@/views/register.vue'
// import Login from '@/views/login.vue'
// import Detail from '@/views/detail.vue'
// import Article from '@/views/article.vue'
// import Collect from '@/views/collect.vue'
// import Like from '@/views/like.vue'
// import User from '@/views/user'

const Detail = () => import('@/views/detail')
const Register = () => import('@/views/register')
const Login = () => import('@/views/login')
const Article = () => import('@/views/article')
const Collect = () => import('@/views/collect')
const Like = () => import('@/views/like')
const User = () => import('@/views/user')
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/article', component: Article },
      { path: '/collect', component: Collect },
      { path: '/like', component: Like },
      { path: '/user', component: User }
    ]
  },
  { path: '/register', component: Register },
  { path: '/login', component: Login },
  { path: '/detail/:id', component: Detail }

]

const router = new VueRouter({
  routes
})

const whiteList = ['/login', '/register']

router.beforeEach((to, from, next) => {
  // console.log(to)
  // console.log(from)
  // console.log(next)
  const token = getItem()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
